package cn.xxc.basic.util;

import cn.xxc.basic.jwt.JwtUtils;
import cn.xxc.basic.jwt.LoginData;
import cn.xxc.basic.jwt.Payload;
import cn.xxc.basic.jwt.RsaUtils;
import cn.xxc.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token != null) {
            try {
                //获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                //解密之后获取载荷数据
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                return payload.getLoginData().getLogininfo();
            }catch (Exception e){
                e.getStackTrace();
            }
        }
        return null;
    }
}
